package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class UserContentDisplayBean {
    private EJBDatabaseQuery ejbdb;
    private String query = null;

    private String f1  = null;  //id
    private String f2  = null;  //gid
    private String f3  = null;  //pid
    private String f4  = null;  //reply
    private String f5  = null;  //title
    private String f6  = null;  //timestamp
    private String f7  = null;  //username
    private String f8  = null;  //size
    private String f9  = null;  //reader
    private String f10 = null;  //reating
    private String f11 = null;  //text
    private String f12 = null;  //image
    private String f13 = null;  //link
    private String f14 = null;  //
    private String f15 = null;  //
    private String f16 = null;  //
    private String f17 = null;  //removed (-1)
    private String f18 = null;  //allow reply (no reply == -1)
    private String f19 = null;  //on top (yes == 0)
    private String f20 = null;  //user_ip

    public StringBuffer adminAddNewForum(HttpServletRequest request, String sub_dir, String table_name, 
					 String mid, String save_call_back) {

	String call_path = request.getContextPath()+request.getServletPath();
	StringBuffer sbf = new StringBuffer();
	String submit = request.getParameter("B");
	if(submit != null) {
	    int db_type = 1;
	    String query = ((new MySqlTable()).getForum(table_name, 1)).toString();
		    
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    int m = -1;
	    try {
		m = ejbdb.update(query);
		if(m != 0) { 
		    sbf.append("\n<BR>ERROR:UserContentDisplayBean:adminAddNewForum:Error");
		    sbf.append("\n<BR>query="+query);
		}
		else {
		    sbf.append("<H3>Forum:"+sub_dir+" And Table:"+table_name+" have been added.</H3>");
		}
	    }
	    catch(Exception e) {
		sbf.append("<BR>Exception:"+e);
	    }
	    return sbf;
	}

	sbf.append("\n<BR><BR><H3>Add New Forum</H3>");
	sbf.append("\n<FORM method='post' action='"+call_path+"'>");
	sbf.append("\n<TABLE border='1'>");
	sbf.append("<TR><TD><B>Forum Name:</B></TD><TD>"+sub_dir+"</TD></TR>");
	sbf.append("<TR><TD><B>Table Name:</B></TD><TD>"+table_name+"</TD></TR>");
	sbf.append("\n</TABLE>");
	sbf.append("<BR><BR>");
	sbf.append("\n<TABLE>");
	sbf.append("\n<TR><TD colspan='2' align='center'>");
	sbf.append("<input type='submit' name='B' value='Add' style='font-size: 8pt'>");
	sbf.append("</TD></TR>");
	sbf.append("\n</TABLE>");
	sbf.append("\n<INPUT type='hidden' name='mid' value='"+mid+"'>");
	sbf.append("\n<INPUT type='hidden' name='aid' value='"+save_call_back+"'>");
	sbf.append("\n</FORM>");
	return sbf;
    }

    public StringBuffer getAddAndSearchBar(String call_path, String mid, boolean admin) {
	StringBuffer sbf = new StringBuffer();

	sbf.append("\n<TABLE cellSpacing='2' cellPadding=0 border=0>");
	sbf.append("\n<TR>");
	sbf.append(MyHtml.makeLinkCell("Add", call_path+"?mid="+mid+"&aid=2"));

	sbf.append("\n<TD align=center bgcolor='#AABBCC' style='border:1 solid #808080' width="+(10+50*6+10)+">");
	sbf.append("\n<Form name='searchForm' method=post action='"+call_path+"?mid="+mid+"&aid=0-1'>");
	sbf.append("\n<input type=text name=sq value='' size=50 maxLength=50 style='font-size:6pt'>");
	sbf.append("\n</TD>");

	sbf.append(MyHtml.makeLinkCell("Search","javaScript:document.searchForm.submit();"));
	sbf.append("\n</FORM>");

	if(admin) {
	    sbf.append(MyHtml.makeLinkCell("Edit", call_path+"?mid="+mid+"&aid=9-1"));
	}
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	return sbf;
    }

    public StringBuffer getUserSubdirInfo (String sub_dir, String user_name) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='2' border='0' ");
	sbf.append(" width='100%' style='border-bottom:1px solid'>");
	sbf.append("\n<TR>");
	sbf.append("\n<TD align='center' style='color:#6600CC' bgcolor='#669999'>");
	sbf.append("<B>"+user_name.toUpperCase()+"'s/"+sub_dir+"</B></TD>");
	sbf.append("\n<TD align='center' style='color:#6600CC' bgcolor='#999966'>");
	sbf.append("Manager:&nbsp;["+user_name+"]</TD>");
	sbf.append("\n<TD align='center' style='color:#6600CC' bgcolor='#669999'>");
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	return sbf;
    }

    public StringBuffer listContent(HttpServletRequest request, String max_rows, 
				    String user_name, String table_name, String mid, String pid, 
				    boolean admin, String link_call_back) {
	StringBuffer sbf  = new StringBuffer();
	StringBuffer error = new StringBuffer();
	try {
	    String call_path = request.getContextPath()+request.getServletPath();

	    if(admin) {
		query ="SELECT LIMIT "+(Integer.parseInt(pid)-1)*Integer.parseInt(max_rows)+" "+
		    max_rows+" * FROM "+table_name +" ORDER BY FIELD2 DESC, FIELD3, FIELD1 DESC, FIELD7;";
	    }
	    else {
		query ="SELECT LIMIT "+(Integer.parseInt(pid)-1)*Integer.parseInt(max_rows)+" "+
		    max_rows+" * FROM "+table_name +" WHERE FIELD7='"+user_name+"' AND FIELD17 <> '-1' "+
		    " ORDER BY FIELD2 DESC, FIELD3, FIELD1 DESC;";
	    }

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    ArrayList alist = ejbdb.select(query);
	    sbf.append("\n<TABLE cellSpacing='0' cellPadding='0' border='0' width='100%'>");
	    int m =0;
	    Iterator it = alist.iterator();
	    ArrayList a = null;
	    boolean is_dir = false;
	    String bgcolor = "#EEEEEE";
	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		f1 = (String)a.get(0);
		f2 = (String)a.get(1);
		f3 = (String)a.get(2);
		f4 = (String)a.get(3);
		f5 = (String)a.get(4);
		f17= (String)a.get(16);
		f18= (String)a.get(17);

		if(f5 != null && f5.indexOf("\"") != -1) {
		    f5 = f5.replaceAll("\"","&quot;");
		}
		    
		if(f5 != null && f5.indexOf("<") != -1) {
		    f5 = f5.replaceAll("<","&lt;");
		}
		if(f5 != null && f5.indexOf(">") != -1) {
		    f5 = f5.replaceAll(">","&gt;");
		}

		f6 = (String)a.get(5);
		f7 = (String)a.get(6);
		f8 = (String)a.get(7);
		f9 = (String)a.get(8);
		f10= (String)a.get(9);
		f11= (String)a.get(10);

		if(f3.indexOf("-") == -1) {
		    if(bgcolor.equals("#EEEEEE")) {
			bgcolor = "#FFFFCC";
		    }
		    else {
			bgcolor = "#EEEEEE";
		    }
		}

		sbf.append("\n<TR><TD width='100%' align='left'>");
		sbf.append("\n\n<table cellSpacing='2' cellPadding='2' border='0'>");

		if(f3.indexOf("-") == -1) {
		    sbf.append("\n<tr>");
		    sbf.append("\n<td valign='top'><img src='/images/blue_star.gif' border='0' align='top'>");
		    sbf.append("\n</td>");
		    sbf.append("\n<td align='left' valign='top'>");

		    if(f17 != null && f17.equals("-1")) {
			sbf.append("\n<A href='"+call_path+"?mid="+mid+"&aid="+link_call_back+"&f1="+f1+"'>"+f5+"</A>[D]");
		    }
		    else {
			sbf.append("\n<A href='"+call_path+"?mid="+mid+"&aid="+link_call_back+"&f1="+f1+"'>"+f5+"</A>");
		    }
		    sbf.append("\n</td>");
		}
		else {
		    m = 30*(f3.length()-(f3.replaceAll("-","")).length());

		    sbf.append("\n<tr><td align='left' valign='top'>");
		    sbf.append("\n<img src='/images/space_line.gif' width='"+m+"' height='1' border='0'>");
		    sbf.append("\n</td><td><img src='/images/child.gif' border='0' align='top'>");
		    sbf.append("\n</td>");
		    sbf.append("\n<td align='left' valign='top'>");

		    if(f17 != null && f17.equals("-1")) {
			sbf.append("\n<A href='"+call_path+"?mid="+mid+"&aid="+link_call_back+"&f1="+f1+"'>"+f5+"</A>[D]");
		    }
		    else {
			sbf.append("\n<A href='"+call_path+"?mid="+mid+"&aid="+link_call_back+"&f1="+f1+"'>"+f5+"</A>");
		    }
		    sbf.append("\n</td>");
		}

		f6 = f6.replaceAll("-", "");

		sbf.append("\n<td valign='top' align='left'>");
		sbf.append("\n<td valign='top'>("+f8+"bytes)</td>");
		sbf.append("\n<td valign='top'>"+f6.substring(0,8)+"</td>");
		sbf.append("</tr>");
		
		sbf.append("\n</table>\n\n");
		sbf.append("\n</TD></TR>");
	    }
	    sbf.append("\n</TABLE>");
	}
	catch (Exception e) {
	    return error.append(e);
	}
	return sbf;
    }

    public StringBuffer listSearchResults(String mid, String squery, String table_name, String user_name, boolean admin, String pid, 
					  String max_rows, String[] field, String detail_call_back) {
	
	StringBuffer sbf = new StringBuffer();
	
	if(squery==null || squery.length()<1) {
	    sbf.append("<BR>");
	    sbf.append(MyHtml.makeLinkButton("Back", "javaScript:history.go(-1);"));
	    return sbf;
	}

	try {
	    if(squery.indexOf("'") != -1) {
		squery = squery.replaceAll("'", "''");
	    }

	    StringBuffer qsbf = new StringBuffer("SELECT LIMIT ");

	    if(admin) { 
		qsbf.append(" "+(Integer.parseInt(pid)-1)*Integer.parseInt(max_rows)+" ");
		qsbf.append(" "+ max_rows+" * FROM "+table_name +" WHERE (");
	    }
	    else {
		qsbf.append(" "+(Integer.parseInt(pid)-1)*Integer.parseInt(max_rows));
		qsbf.append(" "+max_rows+" * FROM "+table_name +" WHERE FIELD7='"+user_name+"' AND FIELD17 <> '-1' AND (");
	    }

	    if(squery.startsWith("\"") && squery.endsWith("\"")) {
		squery = squery.substring(1, squery.length()-1);
		for (int i=0; i<field.length;i++) {
		    qsbf.append(field[i]+" LIKE '%"+squery);
		    if(i == field.length-1) {
			qsbf.append("%';");
		    }
		    else {
			qsbf.append("%' OR ");
		    }
		}
	    }
	    else {
		if(squery.indexOf(" ") != -1) {
		    squery = squery.replaceAll(" ", "%' OR FIELD LIKE '%");
		    for (int i=0; i<field.length;i++) {
			qsbf.append(field[i]+" LIKE '%"+squery.replaceAll("FIELD", field[i])+"%'");
			if(i == field.length-1) {
			    qsbf.append(");");
			}
			else {
			    qsbf.append(" OR ");
			}
		    }
		}
		else {
		    for (int i=0; i<field.length;i++) {
			qsbf.append(field[i]+" LIKE '%"+squery+"%'");
			if(i == field.length-1) {
			    qsbf.append(");");
			}
			else {
			    qsbf.append(" OR ");
			}
		    }
		}
	    }

	    query = qsbf.toString();

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    ArrayList alist = ejbdb.select(query);

	    StringBuffer sbfh = new StringBuffer();
	    sbfh.append("\n<TABLE border=0 align=center>");
	    sbfh.append("\n<TR>");
	    sbfh.append(MyHtml.makeLinkCell("Back", "javaScript:history.go(-1);"));
	    sbfh.append("\n</TR></TABLE>");

	    sbf.append(MyHtml.searchCounter(alist.size()));
	    sbf.append(sbfh);

	    sbf.append("\n<TABLE cellSpacing='0' cellPadding='0' border='0' width='100%'>");
	    int m =0;
	    Iterator it = alist.iterator();
	    ArrayList a = null;
	    boolean is_dir = false;
	    String bgcolor = null;
	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		f1 = (String)a.get(0);
		f2 = (String)a.get(1);
		f3 = (String)a.get(2);
		f4 = (String)a.get(3);
		f5 = (String)a.get(4);

		if(f5 != null && f5.indexOf("\"") != -1) {
		    f5 = f5.replaceAll("\"","&quot;");
		}
		    
		if(f5 != null && f5.indexOf("<") != -1) {
		    f5 = f5.replaceAll("<","&lt;");
		}
		if(f5 != null && f5.indexOf(">") != -1) {
		    f5 = f5.replaceAll(">","&gt;");
		}

		f6  = (String)a.get(5);
		f7  = (String)a.get(6);
		f8  = (String)a.get(7);
		f9  = (String)a.get(8);
		f10 = (String)a.get(9);
		f11 = (String)a.get(10);

		if(bgcolor == null || bgcolor.equals("#EEEEEE")) {
		    bgcolor = "#FFFFCC";
		}
		else {
		    bgcolor = "#EEEEEE";
		}

		sbf.append("\n<TR><TD width='100%' align='left'>");

		sbf.append("\n\n<table cellSpacing='2' cellPadding='2' border='0' style='font-size:10pt'>");

		sbf.append("\n<tr><td align='left' valign='top' width='0'>");
		sbf.append("\n<img src='/images/space_line.gif' width='0' height='1' border='0'>");
		sbf.append("\n</td><td width='10' valign='top'>");
		sbf.append("\n<img src='/images/blue_star.gif' width='10' height='30' border='0' align='top'>");
		sbf.append("\n</td>");

		sbf.append("\n<td style='font-size: 10pt' align='left' valign='top'>");
		sbf.append("\n<A href=i?mid="+mid+"&aid="+detail_call_back+"&f1="+f1+">"+f5+"</A>");
		sbf.append("\n</td>");

		sbf.append("\n<td style='font-size: 10pt' valign='top' align='left'>");
		
		if(admin) {
		    sbf.append("\n<B>"+f7+"</B></td>");
		}

		if(f5.length()+f7.length()>50) {
		    sbf.append("\n<td style='font-size: 8pt' valign=top width="+(f8.length()+8)*6+">("+f8+"-bytes)</td>");
		    //sbf.append("\n<td style='font-size: 8pt' valign=top width="+(f9.length()+8)*6+">("+f9+"-reads)</td>");
		    sbf.append("\n<td style='font-size: 8pt' valign=top width=103>"+f6.substring(0,19)+"</td></tr>");
		}
		else {
		    sbf.append("\n<td style='font-size: 8pt' valign=top>("+f8+"-bytes)</td>");
		    //sbf.append("\n<td style='font-size: 8pt' valign=top>("+f9+"-reads)</td>");
		    sbf.append("\n<td style='font-size: 8pt' valign=top>"+f6.substring(0,19)+"</td></tr>");
		}
		sbf.append("\n</table>\n\n");
		sbf.append("\n</TD></TR>");
	    }
	    
	    sbf.append("\n</TABLE>");
	    if(alist.size()>0) {
		sbf.append(sbfh);
	    }
	}
	catch (Exception e) {
	    System.out.println("UserContentDisplayBean:listSearchResult:Error:"+e);
	}
	return sbf;
    }

    public StringBuffer addNewPost(HttpServletRequest request, HttpServletResponse response, String user_name, boolean admin) {
	String call_path     = request.getContextPath()+request.getServletPath();

	HttpSession session  = request.getSession(true);
	int text_size  = 5000*1000;
	int link_size  = 200;
	int image_size = 5000*1000;

	String text_max = request.getParameter("text_max");
	if(text_max != null) {
	    text_size = Integer.parseInt(text_max);
	}

	String image_max = request.getParameter("image_max");
	if(image_max != null) {
	    image_size = Integer.parseInt(image_max);
	}

	String mid = request.getParameter("mid");
	String aid = request.getParameter("aid");
	StringBuffer error = null;

	String table_name =  request.getParameter("table_name");
	String user_ip = request.getRemoteAddr();

	String fid     = request.getParameter("f1");

	String title   = null; //title
	String poster  = null;  //postname
	String passwd  = null;  //password
	String text    = null; //text
	String img_id  = null; //image
	String link    = null; //link
	String reply   = null; //allow reply
	String on_top  = null; //top position do not count if==0

	String B1        = request.getParameter("B1");  // submit button
	String submit_id = request.getParameter("submit_id");

	if(B1 == null) {
	    submit_id = System.currentTimeMillis()+"";
	}
	else { // this is submit
	    if(submit_id == null || Long.parseLong(submit_id) >System.currentTimeMillis()) {
		return new StringBuffer("");
	    } 
	    else {
		if(session.getAttribute("submit_id") != null && submit_id.equals((String)session.getAttribute("submited_id"))) {
		    error = new StringBuffer("<BR><BR>Your post has been saved. <BR>Please do not re-submit.<BR>");
		    error.append("To add a new post, please go back and clieck the \"Add\" button.");
		    error.append("<BR><BR><BR>");
		    error.append(MyHtml.makeLinkButton("Back", "i?mid="+mid));
		    return error;
		}
	    }

	    fid     = request.getParameter("f1");  //id
	    title   = request.getParameter("f5");  //title
	    poster  = request.getParameter("f7");  //postname
	    passwd  = request.getParameter("f10");  //password
	    text    = request.getParameter("f11"); //text
	    img_id  = request.getParameter("f12"); //image
	    link    = request.getParameter("f13"); //link
	    reply   = request.getParameter("f18"); //allow reply
	    on_top  = request.getParameter("f19"); //top position do not count if==0

	    if(reply == null) {
		reply = "";
	    }
	    if(on_top == null) {
		on_top = "";
	    }

	    if(error == null && (title == null || title.length()<1)) {
		error = new StringBuffer("Title is required.");
	    }
	    else {
		if(title.length()>100) {
		    error = new StringBuffer("Title is too long.");
		}
		else {
		    String temp = null;
		    StringTokenizer tokens = new StringTokenizer(title, " ");
		    while (tokens.hasMoreTokens()) {
			temp = (String)tokens.nextToken();
			if(temp.length()>29) {
			    error = new StringBuffer("Field [Title] Error:["+temp.substring(0,28)+"...].<BR>");
			    break;
			}
		    }
		}

		if(error == null) {
		    if(title.indexOf("\"") != -1) {
			title = title.replaceAll("\"","'&quot;");
		    }
		    
		    if(title.indexOf("'") != -1) {
			title = title.replaceAll("'","''");
		    }
		}
	    }

	    if(error == null) {
		if(text != null) {
		    if(text.length()>text_size) {
			error = new StringBuffer("Text is too long. Max ["+text_size+"]");
		    }
		}
	    }

	    if(error == null) {
		if(link != null) {
		    if(link.length()>4) {
			if(!MyUserInputCheck.isUrl(link)) {
			    error = new StringBuffer("Error found at field [Link].");
			}
		    }
		}
	    }

	    if(error == null) {
		if(text != null && text.indexOf("'") != -1) {
		    text = text.replaceAll("'","''");
		}
	    }

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    if(error == null) {
		if(savePostData(request, user_name)) {
		    session.setAttribute("submited_id", submit_id);
		    session.setAttribute("count", null);
		    session.setAttribute("count",null);
		    
		    try{
			RequestDispatcher rd = request.getRequestDispatcher(request.getServletPath()+"?mid="+mid+"&aid=0"); 
			rd.forward(request,response);
		    }
		    catch(Exception e) {
			System.out.println("UserContentDisplayBean:Error:"+e);
			error = new  StringBuffer("Save data error!");
		    }
		}
		else {
		    error = new StringBuffer("Save data error!");
		}
	    }
	}

	//=========================================================================
	// add or reply post form
	//=========================================================================
	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<BR>");
	
	if(fid == null) {
	    sbf.append(MyHtml.makeLinkButton("Back", "javascript:history.go(-1);"));
	}

	if(error != null && error.length()>0) {
	    sbf.append("\n<TABLE cellSpacing='0' cellPadding='3' border='1'>");
	    sbf.append("\n<TR><TD align='center'>");
	    sbf.append("\n<img src='/images/star.gif' border='0'><I>"+error+"</I></TD></TR>");
	    sbf.append("\n</TABLE>");
	}
	
	sbf.append("\n<form method='post' name='postform'  action='"+call_path+"'>");
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='1' border='1'>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='center'><B>Title(*)</B></TD>");

	if(title==null) {
	    title = "";
	}
	sbf.append("\n<TD colspan=3><input type='text' name='f5' ");
	sbf.append(" value=\""+title+"\" size='60' maxLength='100'></TD>");
	sbf.append("\n</TR>");
	
	sbf.append("\n<TR>");
	sbf.append("\n<TD align='center'><B>Text</B></TD>");
	
	if(text==null) {
	    text = "";
	}
	sbf.append("\n<TD colspan=3><TEXTAREA name=f11 rows=10 cols=56>"+text+"</TEXTAREA></TD>");
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align=center><B>Link</B></TD>");

	if(link==null) {
	    link = "";
	}
	sbf.append("\n<TD colspan='3'><input type='text' name='f13' value=\""+link+"\" ");
	sbf.append(" size='60' maxLength='500'></TD>");
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='center' colspan='4'><input type='submit' name='B1' value='Submit' style='font-size: 8pt'>");
	sbf.append("\n<input type='reset' value='Reset' name='B2' style='font-size: 8pt'>");
	sbf.append("\n</TD></TR>");
	sbf.append("\n</TABLE>");

	sbf.append("\n<input type='hidden' name='mid' value='"+mid+"'>");
	sbf.append("\n<input type='hidden' name='aid' value='"+aid+"'>");
	sbf.append("\n<input type='hidden' name='submit_id' value='"+submit_id+"'>");
       
	if(fid != null && fid.length()>0) {
	    sbf.append("\n<input type='hidden' name='f1' value="+fid+">");
	}
	
	sbf.append("\n</form><BR>");
	return sbf;
    }

    public boolean savePostData (HttpServletRequest request, String user_name) {
	StringBuffer error = new StringBuffer();

	String table_name =  request.getParameter("table_name");
	String user_ip = request.getRemoteAddr();
	
	f1  = request.getParameter("f1"); 
	f5  = request.getParameter("f5");  //title
	f7  = request.getParameter("f7");  //postname
	f11 = request.getParameter("f11"); //text
	f13 = request.getParameter("f13"); //link
	f18 = request.getParameter("f18"); //allow reply
	f19 = request.getParameter("f19"); //top position do not count if==0

	if(f18 == null) {
	    f18 = "";
	}
	if(f19 == null) {
	    f19 = "";
	}

	if(f5 != null && f5.indexOf("'") != -1) {
	    f5 = f5.replaceAll("'","''");
	}

	if(f11 != null && f11.indexOf("'") != -1) {
	    f11 = f11.replaceAll("'","''");
	}

	if(f13 != null && f13.indexOf("'") != -1) {
	    f13 = f13.replaceAll("'","''");
	}

	if (ejbdb == null) {
	    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	}

	try {
	    f8  = ""+f11.length();
	    f9  = "0";  //readers                     
	    f10 = "0";  //rating
	    f6  = "now";         
	    f20 = user_ip; 

	    if(f7 == null) {
		f7 = user_name;
	    } 

	    if(f1 == null) {// this is a new post
		String temp = null;
		if(f19.equals("0")) {
		    temp = ejbdb.selectSingleFieldValue("SELECT MAX(FIELD2) FROM "+table_name+" WHERE FIELD7='"+user_name+"';", "FIELD2");
		}
		else {
		    temp = ejbdb.selectSingleFieldValue("SELECT MAX(FIELD2) FROM "+table_name+
							" WHERE FIELD7='"+user_name+"' AND FIELD19 <> '0';", "FIELD2");
		}

		if(temp == null) {
		    if(f19.equals("0")) {
			f2 = "999999999";
		    }
		    else {
			f2 ="0";
		    }
		}
		else {
		    f2 = (Integer.parseInt(temp)+1)+"";
		}
		f3 = f2;

		query = "INSERT INTO "+table_name+"(FIELD1, FIELD2, FIELD3, FIELD4, FIELD5,"+
		    "FIELD6, FIELD7,FIELD8,FIELD9, FIELD10, FIELD11, FIELD13, FIELD18,FIELD19, FIELD20) VALUES("+
		    null+","+f2+",'"+f3+"',"+"0,'"+f5+"','now','"+f7+"','"+f8+"','"+
		    f9+"','"+f10+"',"+"'"+f11+"','"+f13+"','"+f18+"','"+f19+"','"+f20+"');";

		if(ejbdb.insert(query) != 1) {
		    error.append("\n<BR>Error:Insert:query="+query);
		    return false;
		}
	    }
	    else { // this is reply
		query = "SELECT * FROM "+table_name+" WHERE FIELD1="+f1+" AND FIELD7='"+user_name+"';";
		ArrayList a = ejbdb.selectSingleRecord(query);

		if(a != null && a .size() >=11) {
		    f2 = (String)a.get(1);
		    f3 = (String)a.get(2);
		    f4 = (String)a.get(3);
		    f19 = (String)a.get(18); //if parent is top child must be top
		}

		f3 = f3+"-"+(10+Integer.parseInt(f4));
		
		query = "INSERT INTO "+table_name+"(FIELD1, FIELD2, FIELD3, FIELD4, FIELD5,"+
		    "FIELD6, FIELD7,FIELD8,FIELD9, FIELD10, FIELD11, FIELD13, FIELD18,FIELD19, FIELD20) VALUES("+
		    null+","+f2+",'"+f3+"','0','"+f5+"','now','"+f7+"','"+f8+"','"+f9+"','"+f10+"',"+"'"+f11+"','"+
		    f13+"','"+f18+"','"+f19+"','"+f20+"');";
	    
		if(ejbdb.insert(query) != 1) {
		    error.append("\n<BR>Error:Insert:query="+query);
		    return false;
		}

		//update reply counter
		f4 = (Integer.parseInt(f4)+1)+"";
		query = "UPDATE "+table_name +" SET FIELD4='"+f4+"' WHERE FIELD1="+f1+";";
		if(ejbdb.update(query) != 1) {
		    return false;
		}
	    }
	}
	catch (Exception e) {
	    error.append(e);
	    return false;
	}
	return true;
    }
    
    public StringBuffer displayDetail (HttpServletRequest request, String call_path, String mid, String table_name, String fid, 
				       String pid, boolean manager, boolean admin, String user_name, StringBuffer add_form_bf, 
				       boolean print) {
	
	HttpSession session  = request.getSession(true);
	StringBuffer sbf = new StringBuffer("");

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    if(admin) {
		query = "SELECT * FROM "+table_name+" WHERE FIELD1="+fid+";";
	    }
	    else {
		query = "SELECT * FROM "+table_name+" WHERE FIELD7='"+user_name+"' AND FIELD1="+fid+";";
	    }

	    ArrayList a = ejbdb.selectSingleRecord(query);
	    if(a==null || a.size()<11) {
		return sbf;
	    }
	    if(a.get(0)!=null) { 
		f1 = ""+a.get(0);
	    }
	    else {
		f1 ="";
	    }
	    if(a.get(1)!=null) { 
		f2 = ""+a.get(1);
	    }
	    else {
		f2="";
	    }

	    if(a.get(2)!=null) { 
		f3 = ""+a.get(2);
	    }
	    else {
		f3="";
	    }

	    if(a.get(3)!=null) { 
		f4 = ""+a.get(3);
	    }
	    else {
		f4 ="";
	    }
	    if(a.get(4)!=null) { 
		f5 = ""+a.get(4);
	    }
	    else {
		f5="";
	    }

	    if(a.get(5)!=null) { 
		f6 = ""+a.get(5);
	    }
	    else {
		f6="";
	    }

	    if(a.get(6)!=null) { 
		f7 = ""+a.get(6);
	    }
	    else {
		f7 ="";
	    }
	    if(a.get(7)!=null) { 
		f8 = ""+a.get(7);
	    }
	    else {
		f8="";
	    }

	    if(a.get(8)!=null) { 
		f9 = ""+a.get(8);
	    }
	    else {
		f9="";
	    }
	    if(a.get(9)!=null) { 
		f10 = ""+a.get(9);
	    }
	    else {
		f10 ="";
	    }

	    if(a.get(10)!=null) { 
		f11 = ""+a.get(10);
	    }
	    else {
		f11="";
	    }

	    if(a.get(12) != null) { 
		f13 = ""+a.get(12);
	    }
	    else {
		f13="";
	    }

	    if(a.get(16) != null) { 
		f17 = ""+a.get(16);
		if(!admin && f17.equals("-1")) {
		    return MyMessage.tableMessage("File not found!", 12);
		}
	    }

	    if(a.get(17)!=null) { // no replay if ==-1 
		f18 = ""+a.get(17);
	    }
	    else {
		f18="";
	    }
	    if(a.get(19)!=null) { //ip
		f20 = ""+a.get(19);
	    }
	    else {
		f20="";
	    }

	    if(f5 != null && f5.indexOf("\"") != -1) {
		f5 = f5.replaceAll("\"","&quot;");
	    }

	    if(f5 != null && f5.indexOf("<") != -1) {
		f5 = f5.replaceAll("<","&lt;");
	    }
	    if(f5 != null && f5.indexOf(">") != -1) {
		f5 = f5.replaceAll(">","&gt;");
	    }

	    
	    if(f11 != null && f11.indexOf("<") != -1) {
		f11 = f11.replaceAll("<","&lt;");
	    }

	    if(f11 != null && f11.indexOf(">") != -1) {
		f11 = f11.replaceAll(">","&gt;");
	    }
   
	    if(f11 != null && f11.indexOf("&lt;PRE&gt;") != -1) {
		f11 = f11.replaceAll("&lt;PRE&gt;","<PRE>");
	    }

	    if(f11 != null && f11.indexOf("&lt;/PRE&gt;") != -1) {
		f11 = f11.replaceAll("&lt;/PRE&gt;","</PRE>");
	    }

	    if(f11 != null && f11.indexOf("%IMAGE%") != -1) {
		f11 = f11.replaceAll("%IMAGE%","<img ");
	    }

	    if(f11 != null && f11.indexOf("%/IMAGE%") != -1) {
		f11 = f11.replaceAll("%/IMAGE%",">");
	    }

	    StringBuffer bodysbf = new StringBuffer("");

	    String temp = null;
	    int m  = 0;
	    int n  = 0;
	    int ws = 0;
	    char c = 'a';
	    
	    if(f11 != null && f11.length()>1) {
		bodysbf  = new StringBuffer("");
		StringTokenizer tokens = new StringTokenizer(f11, "\n");
		while (tokens.hasMoreTokens()) {
		    ws =0;
		    temp = (String)tokens.nextToken();
		    n= temp.length();
		    for(m=0; m<n;m++) {
			c = temp.charAt(m);
			if(c == ' ') {
			    ws++;
			}
			else if(c == '\t') {
			    ws = ws+4;
			}
			else {
			    break;
			}
		    }

		    if(ws>0) {
			bodysbf.append("<img src=/images/space.gif width="+ws*10+" height=1>");
		    }

		    bodysbf.append(temp+"<BR>");
		}

		f11 = bodysbf.toString();
	    }

	    ////////////////////////////////////////////////////////////////////
	    // Manager and admin bar
	    ////////////////////////////////////////////////////////////////////

	    StringBuffer msbf = new StringBuffer();
	    msbf.append("\n<TABLE border='0' align='center'>");
	    msbf.append("\n<TR>");

	    msbf.append(MyHtml.makeLinkCell("Delete", call_path+"?mid="+mid+"&f1="+fid+"&aid=3-2&pid="+pid));
	    msbf.append(MyHtml.makeLinkCell("Edit", call_path+"?mid="+mid+"&f1="+fid+"&aid=3-1&pid="+pid));

	    if(admin) {
		msbf.append(MyHtml.makeLinkCell("Admin Edit", call_path+"?mid="+mid+"&f1="+fid+"&aid=4-1&pid="+pid));
		msbf.append(MyHtml.makeLinkCell("Admin Delete", call_path+"?mid="+mid+"&f1="+fid+"&aid=4-2&pid="+pid));
	    }

	    //=================================================
	    // print, e-mail, back line
	    //=================================================

	    msbf.append(MyHtml.makeLinkCell("Print", "'"+request.getContextPath()+"/Print' target='_blank'"));
	    msbf.append(MyHtml.makeLinkCell("Back", "javascript:history.go(-1);"));
	    msbf.append("\n</TR></TABLE>");
	    
	  
	    //==================================================
	    // set print detail
	    //==================================================

	    StringBuffer dsbf = null;
	    dsbf = new StringBuffer();
	    dsbf.append("<HTML><HEAD><TITLE>3WCenter.com ["+f5+"]</TITLE>");
	    dsbf.append("<SCRIPT>");
	    dsbf.append("window.resizeTo(800,500);");
	    dsbf.append("</SCRIPT>");
	    dsbf.append("</HEAD><BODY>");
	    dsbf.append("<TABLE border=0 width=100%>");
	    dsbf.append("<TR><TD align=center><H3>"+f5+"</H3></TD></TR>");
	    dsbf.append("<TR><TD align=center>");
	    dsbf.append("<I>Posted By:&nbsp;<B>"+f7+"</B>&nbsp;"+f6+"</I></TD></TR>");
	    dsbf.append("<TR><TD align=center>&nbsp;</TD></TR>");
	    dsbf.append("</TABLE>");		
	    dsbf.append("<TABLE border=0 width=100%>");
	    dsbf.append("<TR><TD><img src=/images/space.gif width=50 border=0></TD>");
	    dsbf.append("<TD align=left>");
	    dsbf.append(f11);
	    dsbf.append("</TD>");
	    dsbf.append("<TD><img src=/images/space.gif width=50 broder-0></TD>");
	    dsbf.append("</TR>");
	    dsbf.append("<TR><TD align=center colspan=3>");
	    dsbf.append("<BR><BR><img src='/images/copy_right.gif' border='0'>");
	    dsbf.append("</TD></TR>");
	    dsbf.append("</TABLE>");		
	    dsbf.append("</BODY></HTML>");

	    session.setAttribute("PrintBuffer",dsbf);

	    sbf.append("\n<TABLE align='center' valign='top' width=100%>");
	    sbf.append("\n<TR><TD bgcolor='#999966' style='BORDER-BOTTOM: 1px solid;'>"+msbf+"</TD></TR>");
	    sbf.append("\n<TR><TD align=center style='BORDER-BOTTOM: 1px solid;'><BR><BR>");
	    sbf.append("\n<TABLE width=90%>");
	    sbf.append("\n<TR><TD align='center'><h3>"+f5+"</h3></TD></TR>");
	    sbf.append("\n<TR><TD align='center'>");
	    sbf.append("\n<I>Posted By:&nbsp;<B>"+f7+"</B>&nbsp;"+f6+"</I></TD></TR>");
	    sbf.append("\n<TR><TD align=left>");
	    sbf.append(f11);
	    sbf.append("\n</TD></TR>");
	    sbf.append("\n</TABLE><BR><BR>");
	    sbf.append("\n</TD></TR>");
	    sbf.append("\n<TR><TD bgcolor='#999966' style='BORDER-BOTTOM: 1px solid;'>"+msbf+"</TD></TR>");
	    sbf.append("\n</TABLE>");
	    
	    if(f13 != null && f13.length()>4) {
		sbf.append("<TABLE cellSpacing='0' cellPadding='0' align='center' width='100%'>");
		sbf.append("<TR><TD align='center'>The following page is a link from:</TD></TR>");
		String link_name = null;
		if(f13.length()>40) {
		    link_name = f13.substring(0,40);
		}
		else {
		    link_name = f13;
		}
		sbf.append("<TR><TD align='center'>[<A href='"+f13+"' target='_blank'>"+link_name+"</A>]</TD></TR>");
		sbf.append("</TABLE>");

		sbf.append("<TABLE cellSpacing='0' cellPadding='0' align='center'>");
		sbf.append("<TR><TD align='center'>");
		sbf.append("<IFRAME src='"+f13+"' width='700' height='400'></IFRAME>");
		sbf.append("</TD></TR>");
		sbf.append("</TABLE>");
	    }

	    if(add_form_bf != null) {
		sbf.append(add_form_bf);
	    }
	}
	catch (Exception e) {
	    System.out.println("UserContentDisplayBean:Error:Exception:"+e);
	    System.out.println("UserContentDisplayBean:Error:Query:"+query);
	}
	return sbf;
    }	

    public StringBuffer searchDetail (String mid, String aid, String pid, String fid, String table_name) {
	StringBuffer error = new StringBuffer("");
	if(fid==null || fid.length()<1) {
	    return error;
	}

	StringBuffer sbf = new StringBuffer("");

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    query = "SELECT * FROM "+table_name+" WHERE FIELD1="+fid+";";

	    ArrayList a = ejbdb.selectSingleRecord(query);
	    if(a==null || a.size()<11) {
		return sbf;
	    }
	    f1 = (String)a.get(0);
	    f2 = (String)a.get(1);
	    f3 = (String)a.get(2);
	    f4 = (String)a.get(3);
	    f5 = (String)a.get(4);
	    f6 = (String)a.get(5);
	    f7 = (String)a.get(6);
	    f8 = (String)a.get(7);
	    f9 = (String)a.get(8);
	    f10= (String)a.get(9);
	    f11= (String)a.get(10);
	    
	    if(f5 != null && f5.indexOf("\"") != -1) {
		f5 = f5.replaceAll("\"","&quot;");
	    }

	    if(f5 != null && f5.indexOf("<") != -1) {
		f5 = f5.replaceAll("<","&lt;");
	    }
	    if(f5 != null && f5.indexOf(">") != -1) {
		f5 = f5.replaceAll(">","&gt;");
	    }

	    if(f11 != null && f11.indexOf("<") != -1) {
		f11 = f11.replaceAll("<","&lt;");
	    }
	    if(f11 != null && f11.indexOf(">") != -1) {
		f11 = f11.replaceAll(">","&gt;");
	    }

	    if(f11 != null && f11.indexOf("[IMAGE]") != -1) {
		f11 = f11.replaceAll("[IMAGE]","<IMG ");
	    }
	    if(f11 != null && f11.indexOf("[/IMAGE]") != -1) {
		f11 = f11.replaceAll("[/IMAGE]",">");
	    }

	    StringBuffer bodysbf = new StringBuffer("");
	    String temp = null;
	    String space = null;
	 
	    if((f11.trim()).length()>1) {
		StringTokenizer tokens = new StringTokenizer(f11, "\n");
		while (tokens.hasMoreTokens()) {
		    temp = (String)tokens.nextToken();
		    space = "";
		    if(temp != null && (temp.trim()).length()>1) {
			if(temp.startsWith(" ")) {
			    for(int m=0; m<(temp.length()-(temp.trim()).length());m++) {
				space +="&nbsp;";
			    }
			    temp = space+temp;
			}
		    }
		    
		    bodysbf.append(temp+"<BR>");
		}
	    }

	    f11 = bodysbf.toString();	    
	    
	    // Print E-mail, Back bar
	    StringBuffer sbfh = new StringBuffer();
	    sbfh.append("\n<TABLE border=0 align=center>");
	    sbfh.append("\n<TR>");
	    sbfh.append(MyHtml.makeLinkCell("Print", "javaScript:parent.mainFrame.print();"));
	    //sbfh.append(makeLinkCell("E-mail", "javaScript:parent.mainFrame.print();"));
	    sbfh.append(MyHtml.makeLinkCell("Back", "javaScript:history.go(-1);"));
	    sbfh.append("\n</TR></TABLE>");
	    
	    // document detail

	    sbf.append("\n<TABLE cellSpacing=0 cellPadding=0 border=0 width=800 align=center>");
	    sbf.append("\n<TR><TD bgcolor='#999966' style='BORDER-BOTTOM: 1px solid;'>"+sbfh+"</TD></TR>");
	    sbf.append("\n<TR><TD align=center style='BORDER-BOTTOM: 1px solid;'><BR><BR>");
	    sbf.append("\n<TABLE cellSpacing=0 cellPadding=0 border=0 width=800>");
	    sbf.append("\n<TR><TD align=center style='font-size:12pt'height=30>"+f5+"</TD></TR>");
	    sbf.append("\n<TR><TD align=center style='font-size:8pt' height=20>");
	    sbf.append("\n<I>Posted By:&nbsp;<B>"+f7+"</B>&nbsp;"+f6+"</I></TD></TR>");
	    sbf.append("\n<TR><TD align=left>");
	    sbf.append("\n<P style='font-size:10pt;margin-left:2.5em;margin-right:2.5em;margin-top:1.0em; margin-bottom: 3.0em;'>");
	    sbf.append(f11);
	    sbf.append("\n</P>");
	    sbf.append("\n</TD></TR>");
	    sbf.append("\n</TABLE><BR><BR>");
	    sbf.append("\n</TD></TR>");
	    sbf.append("\n<TR><TD bgcolor='#999966' style='BORDER-BOTTOM: 1px solid;'>"+sbfh+"</TD></TR>");
	    sbf.append("\n</TABLE>");

	    query = "UPDATE "+table_name+" SET FIELD9='"+(Integer.parseInt(f9)+1)+"' WHERE FIELD1="+f1+";";
	    ejbdb.update(query);
	}
	catch (Exception e) {
	    error.append(e);
	}
	return sbf;
    }	

    public StringBuffer singlePageAdminEdit(HttpServletRequest request, HttpServletResponse response,
					    String mid, String table_name, String aid, String pid) {
	//String call_path = request.getServletPath();
	String call_path     = request.getContextPath()+request.getServletPath();

	StringBuffer error = new StringBuffer("Sorry. Please try again!");
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    query = "SELECT * FROM "+table_name +" WHERE FIELD1="+f1+";";

	    ArrayList a = new ArrayList();
	    a = ejbdb.selectSingleRecord(query);

	    f1 = (String)a.get(0);
	    f2 = (String)a.get(1);
	    f3 = (String)a.get(2);
	    f4 = (String)a.get(3);
	    f5 = (String)a.get(4);
	    f6 = (String)a.get(5);
	    f7 = (String)a.get(6);
	    f8 = (String)a.get(7);
	    f9 = (String)a.get(8);
	    f10= (String)a.get(9);
	    f11= (String)a.get(10);
	    f12 = (String)a.get(11);
	    f13 = (String)a.get(12);
	    f14 = (String)a.get(13);
	    f15 = (String)a.get(14);
	    f16 = (String)a.get(15);
	    f17 = (String)a.get(16);
	    f18 = (String)a.get(17);
	    f19 = (String)a.get(18);
	    f20= (String)a.get(19);

	    if(f5 != null && f5.indexOf("\"") != -1) {
		f5 = f5.replaceAll("\"","&quot");
	    }
	    if(f11 != null && f11.indexOf("\"") != -1) {
		f11 = f11.replaceAll("\"","&quot");
	    }

	    if(f13 != null && f13.indexOf("\"") != -1) {
		f13 = f13.replaceAll("\"","&quot");
	    }

	    sbf.append(MyHtml.makeLinkButton("Back", "javaScript:history.go(-1);"));
	    sbf.append("\n<FORM method=post action='"+call_path+"'>");
	    sbf.append("\n<TABLE cellSpacing=0 cellPadding=1 border=1 style='font-size: 10pt'>");
	    sbf.append("\n<TR><TD align=center>Title*</TD>");
	    sbf.append("\n<TD align=left><input type=text name=f5 value=\""+f5+"\" size=104 maxLength=100></TD></TR>");

	    sbf.append("\n<TR><TD align=center>Posted By*</TD>");
	    sbf.append("\n<TD align=left><input type=text name=f7 value=\""+f7+"\" size=20 maxLength=20></TD></TR>");

	    sbf.append("\n<TR><TD align=center>Text</TD>");
	    sbf.append("\n<TD align=left><TEXTAREA name=f11 rows=10 cols=74>"+f11+"</TEXTAREA></TD>");
	    sbf.append("\n</TR>");
	    
	    sbf.append("\n<TR><TD align=center>Link</TD>");
	    sbf.append("\n<TD align=left><input type=text name=f13 value=\""+f13+"\" size=104></TD></TR>");

	    sbf.append("\n<TR>");
	    sbf.append("\n<TD align=center>Reply</TD>");
	    sbf.append("\n<TD colspan=3>");

	    if(f18 != null && f18.equals("-1")) { 
		sbf.append("&nbsp;&nbsp;Yes<input type=\"radio\" name=\"f18\" value=''>");
		sbf.append("&nbsp;&nbsp;Only myself<input type=\"radio\" name=\"f18\" value=\"-1\" checked>");
	    }
	    else {
		sbf.append("&nbsp;&nbsp;Yes<input type=\"radio\" name=\"f18\" value='' checked>");
		sbf.append("&nbsp;&nbsp;Only myself<input type=\"radio\" name=\"f18\" value=\"-1\">");
	    }
	    sbf.append("</TD>");
	    sbf.append("\n</TR>");

	    sbf.append("\n</TABLE>");
	 
	    sbf.append("\n<input type=submit name=B value=Update style='font-size: 8pt'>");
	    sbf.append("\n<input type=hidden name=mid value="+mid+">");
	    sbf.append("\n<input type=hidden name=f1 value="+f1+">");
	    sbf.append("\n<input type=hidden name=pid value="+pid+">");
	    sbf.append("\n<input type=hidden name=aid value="+aid+">");
	    sbf.append("\n</form>");
	}
	catch (Exception e) {
	    return error;
	}
	return sbf;
    }

    public StringBuffer singlePageUserEdit(HttpServletRequest request, HttpServletResponse response, 
					   String mid, String table_name, 
					   String user_name, String saved_call_back, String pid) {

	String call_path = request.getContextPath()+request.getServletPath();
	String fid = request.getParameter("f1");
	String aid = request.getParameter("aid");

	StringBuffer error = null;
	StringBuffer sbf   = null;

	String submit = request.getParameter("B");

	if(submit != null && submit.equals("Update")) {
	    String title = request.getParameter("f5");
	    if(title == null || title.length()==0) {
		error = new StringBuffer("Title is required!");
	    }
	    if(error == null) {
		if(saveUserEditUpdate(request, user_name)) {
		    try{
			RequestDispatcher rd = request.getRequestDispatcher(request.getServletPath()+"?mid="+mid+"&aid=0"); 
			rd.forward(request,response);
		    }
		    catch(Exception e) {
			System.out.println("UserContentDisplayBean:Error:"+e);
			error = new  StringBuffer("Save data error!");
		    }
		}
		else {
		    error = new StringBuffer("Update Error. Please try again.");
		}
	    }
	}

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    query = "SELECT * FROM "+table_name +" WHERE FIELD7='"+user_name+"' AND FIELD1="+fid+";";

	    ArrayList a = new ArrayList();
	    a = ejbdb.selectSingleRecord(query);
	    if(a == null || a.size() <10) {
		System.out.println(query);
		return null;
	    }

	    f1 = (String)a.get(0);
	    f2 = (String)a.get(1);
	    f3 = (String)a.get(2);
	    f4 = (String)a.get(3);
	    f5 = (String)a.get(4);
	    f6 = (String)a.get(5);
	    f7 = (String)a.get(6);
	    f8 = (String)a.get(7);
	    f9 = (String)a.get(8);
	    f10= (String)a.get(9);
	    f11= (String)a.get(10);
	    f12 = (String)a.get(11);
	    f13 = (String)a.get(12);
	    f14 = (String)a.get(13);
	    f15 = (String)a.get(14);
	    f16 = (String)a.get(15);
	    f17 = (String)a.get(16);
	    f18 = (String)a.get(17);
	    f19 = (String)a.get(18);
	    f20= (String)a.get(19);

	    if(f5 != null && f5.indexOf("\"") != -1) {
		f5 = f5.replaceAll("\"","&quot");
	    }
	    if(f11 != null && f11.indexOf("\"") != -1) {
		f11 = f11.replaceAll("\"","&quot");
	    }

	    if(f13 != null && f13.indexOf("\"") != -1) {
		f13 = f13.replaceAll("\"","&quot");
	    }

	    sbf = new StringBuffer ();
	    sbf.append("<BR>");
	    sbf.append(MyHtml.makeLinkButton("Back", "javaScript:history.go(-1);"));
	    sbf.append("\n<FORM method='post' action='"+call_path+"'>");
	    sbf.append("\n<TABLE cellSpacing='0' cellPadding='1' border='1'>");

	    if(error != null) {
		sbf.append("\n<TR><TD align='center' colspan='2'><img src='/images/star.gif'>");
		sbf.append("<I>"+error+"</I></TD></TR>");
	    }

	    sbf.append("\n<TR><TD align='center'><B>Title(*)</B></TD>");
	    sbf.append("\n<TD align='left'><input type='text' name='f5' value=\""+f5+"\" ");
	    sbf.append(" size='60' maxLength='100'></TD></TR>");

	    sbf.append("\n<TR><TD align='center'><B>Text</B></TD>");
	    sbf.append("\n<TD align='left'><TEXTAREA name='f11' rows='10' ");
	    sbf.append(" cols='40'>"+f11+"</TEXTAREA></TD>");
	    sbf.append("\n</TR>");
	    
	    sbf.append("\n<TR><TD align='center'><B>Link</B></TD>");
	    sbf.append("\n<TD align='left'><input type='text' name='f13' value=\""+f13+"\" ");
	    sbf.append(" size='60'></TD></TR>");
	    sbf.append("\n</TABLE>");
	 
	    sbf.append("\n<input type='submit' name='B' value='Update' style='font-size: 8pt'>");
	    sbf.append("\n<input type='hidden' name='mid' value="+mid+">");
	    sbf.append("\n<input type='hidden' name='f1' value="+f1+">");
	    sbf.append("\n<input type='hidden' name='pid' value="+pid+">");
	    sbf.append("\n<input type='hidden' name='aid' value="+aid+">");
	    sbf.append("\n</form>");
	}
	catch (Exception e) {
	    System.out.println("UserContentDisplayBean:singlePageUserEdit:Error:"+e);
	    return null;
	}
	return sbf;
    }

    public boolean saveUpdate(HttpServletRequest request) {
	StringBuffer error = new StringBuffer();
	try {
	    String pid = request.getParameter("pid");
	    f1 = request.getParameter("f1");
	    //f2 = request.getParameter("f2");
	    //f3 = request.getParameter("f3");
	    //f4 = request.getParameter("f4");
	    f5 = request.getParameter("f5");
	    f7 = request.getParameter("f7");
	    //f8 = request.getParameter("f8");
	    //f9 = request.getParameter("f9");
	    //f10 = request.getParameter("f10");

	    f11 = request.getParameter("f11");
	    f13 = request.getParameter("f13");

	    f18 = request.getParameter("f18"); //reply
	    if(f18==null) {
		f18 = "";
	    }

	    f19 = request.getParameter("f19"); //top
	    if(f19==null) {
		f19 = "";
	    }

	    if(f5 != null && f5.indexOf("'") != -1) {
		f5 = f5.replaceAll("'","''");
	    }
	
	    if(f11 != null && f11.indexOf("'") != -1) {
		f11 = f11.replaceAll("'","''");
	    }

	    if(f13 != null && f13.indexOf("'") != -1) {
		f13 = f13.replaceAll("'","''");
	    }

	    String table_name = request.getParameter("table_name");

	    query = "UPDATE "+table_name+" SET FIELD5='"+f5+"', FIELD7='"+f7+"', FIELD8='"+f11.length()+
		"', FIELD11='"+f11+"', FIELD13='"+f13+"', FIELD18='"+f18+"', FIELD19='"+f19+"'  WHERE FIELD1="+f1+";";
	    
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    int result = 0;
	    result = ejbdb.update(query);
	    if(result != 1) {
		error.append("\n<BR>Error:Update:query="+query);
		return false;
	    }
	}
	catch (Exception e) {
	    return false;
	}
	return true;
    }

    public boolean saveUserEditUpdate(HttpServletRequest request, String user_name) {
	try {
	    f1 = request.getParameter("f1");
	    //f2 = request.getParameter("f2");
	    //f3 = request.getParameter("f3");
	    //f4 = request.getParameter("f4");
	    f5 = request.getParameter("f5");
	    //f7 = request.getParameter("f7");
	    //f8 = request.getParameter("f8");
	    //f9 = request.getParameter("f9");
	    //f10 = request.getParameter("f10");

	    f11 = request.getParameter("f11");
	    f13 = request.getParameter("f13");

	    f18 = request.getParameter("f18"); //reply
	    if(f18==null) {
		f18 = "";
	    }

	    f19 = request.getParameter("f19"); //top
	    if(f19==null) {
		f19 = "";
	    }

	    if(f5 != null && f5.indexOf("'") != -1) {
		f5 = f5.replaceAll("'","''");
	    }
	
	    if(f11 != null && f11.indexOf("'") != -1) {
		f11 = f11.replaceAll("'","''");
	    }

	    if(f13 != null && f13.indexOf("'") != -1) {
		f13 = f13.replaceAll("'","''");
	    }

	    String table_name = request.getParameter("table_name");

	    query = "UPDATE "+table_name+" SET FIELD5='"+f5+"', FIELD8='"+f11.length()+
		"', FIELD11='"+f11+"', FIELD13='"+f13+"', FIELD18='"+f18+"', FIELD19='"+f19+
		"'  WHERE FIELD7='"+user_name+"' AND FIELD1="+f1+";";
	    
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    int result = 0;
	    result = ejbdb.update(query);
	    
	    if(result != 1) {
		System.out.println("\nUserContentDisplayBean:saveUserEditUpdate:Error:[query="+query+"]");
		return false;
	    }
	}
	catch (Exception e) {
	    System.out.println("\nUserContentDisplayBean:saveUserEditUpdate:Error:["+e+"]");
	    return false;
	}
	return true;
    }

    public boolean updateAdPageCounter (String adv_id) {
	if(ejbdb == null) {
	    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	}

	try {
	    String query = "SELECT PAGE_COUNTER FROM ADS WHERE ID="+adv_id+";";
	    String temp = ejbdb.selectSingleFieldValue(query, "PAGE_COUNTER");
	    int count = 1;
	    if(temp != null && temp.length()>0) {
		count = Integer.parseInt(temp);
	    }
	    
	    query = "UPDATE ADS SET PAGE_COUNTER="+(count+1)+" WHERE ID="+adv_id+";";
	    ejbdb.update(query);
	}
	catch(IOException e) {
	    System.out.println("UserContentDisplayBean:updateAdPageCounter:Error:"+e);
	}
	return true;
    }
 }






